package com.augurit.common.base.extmodel;

import java.util.ArrayList;
import java.util.List;

import com.augurit.common.base.action.BaseAction;
import com.augurit.common.util.JsonUtils;
import com.augurit.common.util.lang.StringUtils;

public class ExtEditorGridResult<T>{
	
	// 属性
	private String result;	//用于获取新增或修改对象列表的json字符串 或者 被删除对象ID列表字符串
	
	/**
	 * 将JSON字符串转换为对象列表
	 * @param jsonString
	 * @param formClass
	 * @return
	 * @throws Exception 
	 */
	public List<T> getChangedForms(final Class<T> formClass) throws Exception{
		List<T> result = new ArrayList();
		
		// 根据JSON字符串判断是否存为数组
		int exist = JsonUtils.existBeansFromJson(this.result);
		
		if(exist == 0){
			// 单个对象转换
			result.add(BaseAction.buildFromJson(this.result, formClass));
		}
		else if(exist == 1){
			// 多个对象转换
			List<T> list = BaseAction.buildListFromJson(this.result, formClass);
			result.addAll(list);
		}
		
		return result;
	}
	
	/**
	 * 返回可编辑表格组件要删除的对象ID
	 * @param delIds
	 * @return
	 */
	public Long[] getDeletedIds(){
		if(this.result != null && this.result.trim().length() > 0){
			if(JsonUtils.isArrayJson(this.result)){
				String idsStr = this.result.substring(1, this.result.length() - 1);
				return StringUtils.convertStringArrayToLongArray(idsStr.split(","));
			}else{
				return new Long[]{Long.valueOf(this.result)};
			}
		}else
			return null;
	}
	
	/**
	 * 获取新增操作的提示消息字符串
	 * @param list
	 * @return
	 */
	public static String getEditorGridCreateMessage(final List list){
		String result = "";
		
		int size = list != null && list.size() > 0 ? list.size() : 0;
		
		if(size > 0)
			result = "已成功新增" + list.size() + "条记录！";
		else
			result = "无任何需要新增的记录！";
		
		return result;
	}
	
	/**
	 * 获取修改操作的提示消息字符串
	 * @param list
	 * @return
	 */
	public static String getEditorGridUpdateMessage(final List list){
		String result = "";
		
		int size = list != null && list.size() > 0 ? list.size() : 0;
		
		if(size > 0)
			result = "已成功修改" + list.size() + "条记录！";
		else
			result = "无任何需要修改的记录！";
		
		return result;
	}
	
	/**
	 * 获取新增或修改操作的提示消息字符串
	 * @param list
	 * @return
	 */
	public static String getEditorGridDeleteMessage(final Long[] ids){
		String result = "";
		
		int size = ids != null && ids.length > 0 ? ids.length : 0;
		
		if(size > 0)
			result = "已成功删除" + ids.length + "条记录！";
		else
			result = "无任何需要删除的记录！";

		return result;
	}
	
	public String getResult() {
		return result;
	}
	
	public void setResult(String result) {
		this.result = result;
	}
}
